from django.contrib import admin
from api_App.models import *
# Register your models here.
from django.contrib import admin
import threading
import time
from api_App.views_api import run
from api_App.views import set_monitor_next
import requests
import json
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
# Register your models here.
from api_App.models import *
admin.site.register(DB_notice)
admin.site.register(DB_net_work)
admin.site.register(DB_project_list)
admin.site.register(DB_env_list)
admin.site.register(DB_Api_list)
admin.site.register(DB_power)
admin.site.register(DB_case_set)
admin.site.register(DB_report)
admin.site.register(DB_monitor)
def email(adresss,content):
    "发邮件"
    mail_to = adresss.split(',')
    mail_host = 'smtp.qq.com'
    mail_user = adresss # 发送者邮箱地址
    mail_pass = 'qwjroiqj12j1o2jio' #smtp授权码

    c = MIMEText(content,'html','utf-8')
    msg = MIMEMultipart('related')
    msg['From'] = mail_user
    msg['To'] = mail_to
    msg['Subject'] = '接口测试平台线上监控报告'
    print('发送成功')
    msg.attach(c)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host,25)
        server.login(mail_user,mail_pass)
        server.sendmail(mail_user,mail_to,msg.as_string())
        print('发送邮件成功')
        server.close()
    except:
        print('邮件发送失败')



def robotApi(url,content):
    "机器人消息"
    data = {"msg_type":"text","content":{"text":content}}
    try:
        re = requests.post(url,data=json.dumps(data))
        print(re.text)
    except:
        print('机器人消息发送报错')

def monitor_thread():
    '巡逻线程'
    while True:
        monitors = DB_monitor.objects.all()
        for monitor in monitors:
            if monitor.status != True:
                continue
            if abs( time.time() - float(monitor.step)) < 60:
                print('执行了')
                project_id = monitor.pro_ject_id
                res = run(request='',project_id=project_id)
                set_monitor_next(monitor,'sys')
                monitor.save()
                print('执行了')
                # 判断是否需要发送邮件/机器人消息，根据具体公司的领导要求来做。
                if 'True' in str(res.content):
                    last_report = DB_report.objects.filter(pro_ject_id=project_id)
                    last_report = last_report[len(last_report)-1]
                    r = ''
                    for i in eval(last_report.apis_result):
                        r += '\n' + i['label'] + ' : ' + str(i['result'])
                    content = '【%s】线上监控项目报错！%s '%(monitor.label,r)
                    email(monitor.email,content)
                    robotApi(monitor.robot,content)
t = threading.Thread(target=monitor_thread  )
t.setDaemon(True)
t.start()






